package com.jp.knowledge.logic;

import android.content.Context;

import com.google.gson.JsonObject;
import com.jp.knowledge.http.HttpHelper;
import com.jp.knowledge.model.IModel;
import com.jp.knowledge.model.ProdcutData;
import com.jp.knowledge.util.JsonUtil;
import com.jp.knowledge.util.L;
import com.jp.knowledge.util.ResultSubscriber;

import java.util.List;

/**
 * Created by jiayen on 17/6/9.
 *
 * 产品库  逻辑业务处理
 */

public class ProductFragmentLogic implements ResultSubscriber.OnResultListener {
    private static   ProductFragmentLogic intance;
    private static   Context mContext;

    private SucCallback  callback;

    private  final int EXP_LIST_CODE=0x000000001;
    private List<ProdcutData> datas;
    private ProductFragmentLogic(){

    }

    public void setCallback(SucCallback callback) {
        this.callback = callback;
    }

    public static  ProductFragmentLogic getIntance(Context context){
                 mContext=context;
        return  intance==null?new ProductFragmentLogic():intance;
    }


    public  List<ProdcutData>  getExpList(int action,int mode,int page,long time){
//        if(datas!=null)return datas;
        //{"action":1,"mode":0,"token":"346d7eeb-802f-4562-803c-b957add125b5","type":1,"time":1496927128844,"page":1}
        //{"action":1,"mode":0,"token":"346d7eeb-802f-4562-803c-b957add125b5","type":1,"time":1496927128844,"page":1}
        JsonObject param = new JsonObject();
        param.addProperty("action",action);
        param.addProperty("mode",mode);
        param.addProperty("type",1);
        param.addProperty("time",time);
        param.addProperty("page",page);
        HttpHelper.getInstance(mContext).expList(param, EXP_LIST_CODE, this);
        return  null;
    }

    @Override
    public void onStart(int requestType) {

    }

    @Override
    public void onCompleted(int requestType) {
        if(callback!=null){
            callback.compelete();
        }
    }

    @Override
    public void onError(int requestType) {

    }

    @Override
    public void onNext(IModel data, int requestType) {
        if(data.getErrcode()==0){
            if(requestType==EXP_LIST_CODE){
//                String str = JsonUtil.getInstance().objToJson(data);
                datas = data.getList(ProdcutData.class);
                if(callback!=null){
                    callback.success(datas);
                }
//                L.e(str);
            }
        }
    }

    public interface   SucCallback{
        void success(List<ProdcutData>  list);

        void   compelete();
    }
}
